
const { resolve } =  require('path');
const { VueLoaderPlugin } = require('vue-loader');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
// const WorkboxWebpackPlugin = require('workbox-webpack-plugin') // PWA渐进网络开发应用程序(离线访问)
// sw代码运行在服务器上
// 模块热替换 HMR hot module replacement
// module.exports = (env, argv) => {

// }
// 移除 打包注释
class MyPlugin {
  apply (complier) {
    console.log('myPlugin 启动')
    complier.hooks.emit.tap('Myplugin', compilation => {
      for (const name in compilation.assets) {
        console.log(name)
        if (name.endsWith('js')) {
          const contents = compilation.assets[name].source()
          const withoutComments = contents.replace(/\/\*\*+\//g, '')
          compilation.assets[name] = {
            source: () => withoutComments,
            size: () => withoutComments.length
          }
        }
      }
    })
  }
}
module.exports = {
  // 入口起点, webpack以那个文件为入口起点开始打包,分析构建内部依赖图
  entry: './src/main.js',
  // 输出
  output: {
    filename: 'js/[name].[hash].js',
    path: resolve(__dirname, './dist')
  },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      "@": resolve(__dirname, "./src")
    }
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  },
  // loader 让 webpack 能够去处理那些非 JavaScript 文件
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        // 处理图片
        test: /\.(jpg|png|gif)$/,
        loader: 'url-loader',
        options: {
          // 图片大小小于8kb,就会被base64处理
          // 优点: 减少请求数量(减轻服务器压力)
          // 缺点: 图片体积会更大(文件请求速度更慢)
          limit: 10 * 1024,
          esModule: false,
          // 给图片进行命名
          // [hash:10] 取图片前10位
          // ext取原来的扩展名
          name: '[hash:10].[ext]',
          outputPath: 'image'
        }
      },
      {
        test: /\.js$/,
        exclude: resolve('node_modules'),
        use: [
          // 'thread-loader',
          {
            loader: 'babel-loader',
            options: {
              presets: [
                ['@babel/env']
              ]
            }
          }
        ]
      },
      {
        test: /\.(css)$/,
        // 使用哪些loader
        use: [
          process.env.NODE_ENV === 'production'
            ? MiniCssExtractPlugin.loader // 创建style标签, 将js的模式资源插入进行,添加到head 中生效
            : 'vue-style-loader',
          // use 数组中loader 执行顺序, 从右到左,从下到上一次执行
          'css-loader', // 将css文件变成commonjs 模块加载到模块中,里面内容是字符串
        ]
      },
      // {
      //   test: /\.scss$/,
      //   use: [
      //     'vue-style-loader',
      //     'css-loader',
      //     'sass-loader'
      //   ]
      // },
      {
        test: /\.less$/,
        use: [
          // 创建style标签
          process.env.NODE_ENV !== 'production'
          ? 'vue-style-loader' // 创建style标签, 将js的模式资源插入进行,添加到head 中生效
          : MiniCssExtractPlugin.loader,
          'css-loader',
          'less-loader',
          {
            loader: 'postcss-loader',
            options: {
              ident: 'postcss',
              plugins: () => [
                require('postcss-preset-env')()
              ]
            }
          }
        ]
      },
      {
        test: /\.(eot|woff2?|ttf|svg)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name]-[hash:5].min.[ext]",
              limit: 3000,
              outputPath: 'font',
            }
          }
        ]
      }
    ]
  },
  plugins: [
    // new CleanWebpackPlugin(),
    // 提取css成单独文件
    new MiniCssExtractPlugin({
      filename: 'css/main.css'
    }),
    // 压缩css
    new OptimizeCssAssetsWebpackPlugin(),
    new VueLoaderPlugin(),
    new MyPlugin(),
    // pwa
    // new WorkboxWebpackPlugin.GenerateSW({
    //   // 帮助serviceworker快速启动
    //   // 删除旧的servicework
    //   clientsClaim: true,
    //   skipWaiting: true
    // })
  ]
}